package cn.itsource.pethome.product.controller;

import cn.itsource.pethome.product.domain.Product;
import cn.itsource.pethome.product.query.ProductQuery;
import cn.itsource.pethome.product.service.IProductService;
import cn.itsource.pethome.utils.AjaxResult;
import cn.itsource.pethome.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Product product){
        if (product.getId() == null){
            productService.save(product);
        }else {
            productService.update(product);
        }
        return AjaxResult.me();
    }

    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        productService.delete(id);
        return AjaxResult.me();
    }

    @GetMapping(value = "/{id}")
    public Product selectById(@PathVariable("id") Long id){
        return productService.loadById(id);
    }

    @GetMapping
    public List<Product> seleceAll(){
        return productService.loadAll();
    }

    @PostMapping
    public PageResult<Product> seleceByPage(@RequestBody ProductQuery query){
        return productService.selectByPage(query);
    }

    @PatchMapping
    public AjaxResult deletes(@RequestBody Long[] ids){
        productService.deletes(ids);
        return AjaxResult.me();
    }

    @PatchMapping("/batchOnSale")
    public AjaxResult batchOnSale(@RequestBody Long[] ids){
        return productService.batchOnSale(ids);
    }

    @PatchMapping("/batchOffSale")
    public AjaxResult batchOffSale(@RequestBody Long[] ids){
        return productService.batchOffSale(ids);
    }

}
